/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.View;

import com.lcfgly.Exception.ErrorException;
import com.lcfgly.Exception.Message;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 控制器父类，统一异常处理
 * @author lcfgly
 */
public class FatherController {
    
    @ExceptionHandler
    public String exp(HttpServletRequest request, Exception ex)
    {
        request.setAttribute("ex", ex);
        if(ex instanceof Message)
            return "exception/message";
        else if(ex instanceof ErrorException)
            return "exception/error";
        else
            return "exception/warn";
    }
    
    /**
     * 返回错误map
     * @param res
     * @return 
     */
    protected Map getError(BindingResult res){
            Map error=new HashMap();
            List<FieldError> ress=res.getFieldErrors();
            for (FieldError object : ress) {
                error.put(object.getField(), object.getDefaultMessage());
            }
            return error;
    }
    
}
